<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/12
 * Time: 12:17
 */
namespace app\admin\controller;

class Dynamic extends Common
{
    /*
     * 旅说管理
     *
     * 旅说列表
     */
    public function lists()
    {
        $db = db('dynamic')->where('del',0);

        $content = input('content');
        if ($content) {
            $db->where('content','like','%'.$content.'%');
        }
        $status = input('status');
        if(strlen($status)){
            $db->where('status',$status);
        }
        $start = input('start');
        $end = input('end');
        if ($start) {
            $db->where('createtime','>=',strtotime($start));
        }
        if ($end) {
            $db->where('createtime','<=',strtotime($end));
        }
        if (strlen(input('type'))) {
            $db->where('type',input('type'));
        }
        if (input('status')) {
            $db->where('status',input('status'));
        }
        if (strlen(input('downline'))) {
            $db->where('downline',input('downline'));
        }
        $page = input('page') > 0 ? input('page/d') : 1;
        $listRows = input('listRows') > 0 ? input('listRows/d') : 10;

        $res =$db->order('top','desc')->order('updatetime','desc')->page($page, $listRows)->select();
       // $db->page($page, $listRows)->order('createtime', 'desc');
        $total = $db->count();
        $this->rest(['list' => $res, 'page' => getPage($page, $listRows, $total)]);
    }

    /*
     * 设置过审时间
     */
    public function settime()
    {
        $min = input('min');
        if(empty($min)){
            $this->rest('参数错误', 's004');
        }
        $res = db('dynamic')->where('id','>',0)->update(['trialtime'=>$min*60]);
        if ($res === false) {
            $this->rest('更新失败', 's004');
        } else {
            $this->rest('更新过审时间成功');
        }
    }

    /*
     * 删除旅说
     */
    public function del()
    {
        $did = input('did');
        $uid = input('uid');
        if(empty($did) || empty($uid)){
            $this->rest('参数错误', 's004');
        }
        $res = db('dynamic')->where(['id'=>$did,'uid'=>$uid])->update(['del'=>1]);
        if ($res) {
            @db('comment')->where('modular', 2)->where(['aid'=>$did])->delete();
            $this->rest('删除成功');
        } else {
            $this->rest('删除失败', 's004');
        }
    }

    /*
     * 旅说  [加精 下线]    置顶状态 [0:未置顶 1:置顶]
     *  帖子状态 [0:正常 1:加精 2:下线]   0: 恢复上线
     */
    public function top()
    {
        $did = input('did');
        $type = input('type');
        $is = input('is');
        if(empty($did) || empty($type) || !strlen($is)){
            $this->rest('参数错误', 's004');
        }
        $update = [
            'updatetime'=>time(),
        ];
        if($type == 'top'){ # 置顶设置
            $update['top'] = $is;
            $this->curd($did,$is,$update);
        }elseif($type == 'status'){ # 加精设置
            $update['status'] = $is;
            $this->curd($did,$is,$update);
        }elseif($type == 'downline'){ # 下线设置
            $update['downline'] = $is;
            $this->curd($did,$is,$update);
        }elseif($type == 'type'){ # 达人设置
            $update['type'] = $is;
            $this->curd($did,$is,$update);
        }
    }

    private function curd($did,$is,$update)
    {
        if($is == 1 || $is ==0){
            $res = db('dynamic')->where('id',$did)->update($update);
            if ($res) {
                $this->rest('操作成功');
            } else {
                $this->rest('操作失败', 's004');
            }
        }
    }

}